package AnswerOfComent;

import structure.TreeNode;

/**
 * 利用完全二叉树的特性
 */
public class LeetCode_countNodes_222 {
    public int countNodes(TreeNode root) {
        if (root == null) {
            return 0;
        }
        // 拿到左子树和右子树的深度
        int leftDepth = getDepth(root.left);
        int rightDepth = getDepth(root.right);
        if (leftDepth == rightDepth) {
            return (1<< leftDepth) + countNodes(root.right);
        } else {
            return (1<< rightDepth) + countNodes(root.left);
        }

    }
    public int getDepth(TreeNode root) {
       int depth = 0;
       while (root != null) {
           root = root.left;
           depth++;
       }
       return depth;
    }
}
